/**
 * reducer这个函数是一个纯函数
 */
import {PUSHMENU,CHANGEMENUSTATUS} from "../constanst/index"
export default function menuReducer(state={menus:[
    {title:"首页",path:"/home/",status:true},
    {title:"角色管理",path:"/home/role",status:false},
]},action){
    switch(action.type){
        case PUSHMENU:
            //先将所有的状态变为false
            state.menus.forEach(item=>item.status = false)
            const menuIndex = state.menus.findIndex(item=>item.path == action.payload.path)
            console.log(menuIndex);
            if(menuIndex !=-1){
                state.menus[menuIndex].status = true
            }else{
                state.menus.push(action.payload)
            }
            return {...state}
        case CHANGEMENUSTATUS:
            state.menus.forEach(item=>item.status = false)
            const index = state.menus.findIndex(item=>item.path == action.payload)
            state.menus[index].status = true
            
            return {...state}
        default:
            return state
    }
}